/*
 * The MIT License
 * 
 * Copyright (c) 2008 Comcast Interactive Media
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cim.sf.core.command.equinox;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Enumeration;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.osgi.framework.Bundle;

import cim.sf.core.command.CommandInvocation;

/**
 * 
 * @author <a href="mailto:jeremy_volkman@cable.comcast.com">Jeremy Volkman</a>
 *
 */
public class CommandInterpreterAdapter implements CommandInterpreter {

    private CommandInvocation invocation;
    private int argIndex = 0;
    
    public CommandInterpreterAdapter(CommandInvocation invocation) {
        this.invocation = invocation;
    }
    
    
    public Object execute(String arg0) {
        throw new UnsupportedOperationException();
    }

    public String nextArgument() {
        if (invocation.args.length > argIndex) {
            return invocation.args[argIndex++];
        } else {
            return null;
        }
    }

    public void print(Object arg0) {
        invocation.out.print(arg0);
    }

    public void printBundleResource(Bundle bundle, String resource) {
        URL entry = null;
        entry = bundle.getEntry(resource);
        if (entry != null) {
            try {
                println(resource);
                InputStream in = entry.openStream();
                byte[] buffer = new byte[1024];
                int read = 0;
                try {
                    while ((read = in.read(buffer)) != -1)
                        print(new String(buffer, 0, read));
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                        }
                    }
                }
            } catch (Exception e) {
                printStackTrace(e);
            }
        } else {
            println("Resource not in bundle.");
        }
    }

    @SuppressWarnings("unchecked")
    public void printDictionary(Dictionary dic, String title) {
        if (dic == null)
            return;

        int count = dic.size();
        String[] keys = new String[count];
        Enumeration keysEnum = dic.keys();
        int i = 0;
        while (keysEnum.hasMoreElements()) {
            keys[i++] = (String) keysEnum.nextElement();
        }
        Arrays.sort(keys);

        if (title != null) {
            println(title);
        }
        for (i = 0; i < count; i++) {
            println(" " + keys[i] + " = " + dic.get(keys[i]));
        }
        println();    
    }

    public void printStackTrace(Throwable arg0) {
        arg0.printStackTrace(invocation.out);
    }

    public void println() {
        invocation.out.println();
    }

    public void println(Object arg0) {
        invocation.out.println(arg0);
    }

}
